<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

/**
 * 保障对象统计报表管理
 * Class ReportOverdueController
 * @package Admin\Controller
 */
class ReportOverdueController extends BaseController {
    /**
     * 系统管理 -> 系统管理 -> 保障对象统计报表管理 -> listview
     */
    public function index(){
        //获取搜索条件
        $filter = $this->fn_GenerateFilter();
        $where = $this->fn_GenerateWhereClause();
        $order = $this->fn_GenerateOrderBy();

        //分页信息
        $pageNum = intval(I("post.page"));
        $pageNum = $pageNum==0?1:$pageNum;
        $page = $this->fn_GetPageInfo($where, $pageNum);
        $this->assign("page", $page);

        //搜索出小区信息
        $limit = ($page['from']-1).",".$page['num'];
        $list = M("house")->where($where)->order($order)->limit($limit)->select();
        for($i=0; $i<count($list); $i++){
            $list[$i]["offset"] = $page['from']-1+$i;
        }
        $this->assign("list", $list);

        //设置搜索条件
        $this->assign("filter", $filter);
        $this->display();
    }

    protected function fn_GetLastYears($num=5){
        $years = array();
        $year = intval(date("Y"));
        for($i=0; $i<$num; $i++){
            $years[] = $year-$i;
        }
        return $years;
    }

    /**
     * 保障对象统计报表管理 -> formview
     */
    public function detail(){

        $years = $this->fn_GetLastYears();
        $this->assign("years", $years);
        $where = array();
        if(I("post.year")!="all"){
            $year = I("post.year")?I("post.year"):date("Y");
            $this->assign("year", $year);
            $year1 = strtotime($year."/01/01 00:00:00");
            $year2 = strtotime($year."/12/31 24:00:00");
            $where["ch.ctime"] = array('between',array($year1, $year2));
        }

        $data = M("charge ch")
            ->join(C("DB_PREFIX")."house h on h.name = ch.house_no", 'LEFT')
            ->join(C("DB_PREFIX")."contact con on con.no = ch.contact_no", 'LEFT')
            ->join(C("DB_PREFIX")."community c on c.id = h.community_id", 'LEFT')
            ->field("c.name as comm_name, con.type as house_type, ch.sfjf as paid, sum(ch.qfje) as total")
            ->where($where)->group("comm_name, house_type, sfjf")->select();
        //echo M()->getLastSql();
        //print_r($data);

        $area = M("community")->field("name")->select();
        $houseSt = array();
        foreach($area as $value){
            $houseSt[$value['name']] = array(
                "廉租房"=>array("owned"=>0, "paid"=>0, "sum"=>0),
                "公租房"=>array("owned"=>0, "paid"=>0, "sum"=>0)
            );
        }

        $total = array("owned"=>0, "paid"=>0, "sum"=>0);
        foreach ($data as $item) {
            if($item["paid"]=="是"){
                $total["paid"] += $item["total"];
                $houseSt[$item['comm_name']][$item['house_type']]["paid"] += $item["total"];
            }else{
                $total["owned"] += $item["total"];
                $houseSt[$item['comm_name']][$item['house_type']]["owned"] += $item["total"];
            }
            $houseSt[$item['comm_name']][$item['house_type']]["sum"] += $item["total"];
            $total["sum"] += $item["total"];
        }
        $this->assign("total", $total);

        $rentalInfo = array();
        foreach ($houseSt as $key => $value) {
            $rentalInfo[] = array("comm_name"=>$key, "data"=>$value);
        }
        $this->assign("houseInfo", $rentalInfo);

        $this->display();
    }
}